#include <iostream>

#include "compile_run.hpp"
#include "../Comm/httplib.h"
using namespace ns_compile_runner;
using namespace httplib;

void Usege(const std::string str)
{
    std::cout << "Usage:" << "\n\t" << "./compile_server port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2){
        Usege(argv[0]);
    }

    Server svr;

    //for debug
    //svr.set_base_dir("./wwwroot");  
    //svr.Get("/hello", [](const Request &req, Response &resp)
    //        { resp.set_content("nihao,用于测试", "text/plain;charset=utf-8"); });
    svr.Post("/compile_and_run", [](const Request &req, Response &res)
             {
                std::string json_in = req.body;
                std::string json_out;
                if (!json_in.empty())
                {
                    compileAndrun::Start(json_in, &json_out);
                    res.set_content(json_out, "application/json;charset=utf-8");
                } });
    
    svr.listen("0.0.0.0", std::stoi(argv[1]));
    
    // for debug
    //************************************** */
    // Json::Value in_root;
    // in_root["code"] = R"(
    //     #include<iostream>
    //     int main()
    //     {
    //         //while(true);
    //         //int *p = new int[1024*1024*1024];
    //         std::cout<<"hello world"<<std::endl;
    //         return 0;
    //     }
    // )";
    // in_root["input"] = "";
    // in_root["cpu_limit"] = 1;
    // in_root["mem_limit"] = 10240 * 3;

    // Json::StyledWriter writer;
    // std::string in_json = writer.write(in_root);

    // std::string out_json;
    // compileAndrun::Start(in_json, &out_json);

    // std::cout << out_json << std::endl;

    //***************************************** */
    return 0;
}